package com.sss.day35_mvc_mybatis.controller;

import com.sss.day35_mvc_mybatis.model.User;
import com.sss.day35_mvc_mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import com.sss.day35_mvc_mybatis.util.R;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * 全世界我最爱朴彩英！
 *
 * @Author sss
 * @Create 2023/11/27 17:46
 */
@Controller
public class Demo4TestMybatis {

    @Autowired
    private UserService service;

    @GetMapping("/one")
    @Transactional
    @ResponseBody
    public R findUserById(int id){
        User user = service.findUserById(id);
        System.out.println(user);
        System.out.println("================");
        User user2 = service.findUserById(id);
        System.out.println(user2);
        if (user != null){
            return R.ok(user);
        }else {
            return R.fail();
        }
    }

    //无条件查全部
    @GetMapping("/all")
    @ResponseBody
    public R findAll(int pageNum,int pageSize){

        List<User> user =  service.findAll();
        return R.ok(user);
    }

    //条件查全部
    @GetMapping("/login")
    @ResponseBody
    public R findByLogin(String username,String password){
        User user = service.findByLogin(username,password);
        return R.ok(user);
    }

    //map查询
    @GetMapping("/map")
    @ResponseBody
    public R findUserByLoginMap(@RequestParam  HashMap<String,Object> map){
        User user = service.findUserByLoginMap(map);
        return R.ok(user);
    }


    //增加
    @GetMapping("/add")
    @ResponseBody
    public R addUser(User user){
        int i = service.addUser(user);
        if (i>0){
            return R.ok();
        }else {
            return R.fail();
        }

    }
}
